﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_left : System.Web.UI.Page
{
    public override string StyleSheetTheme
    {
        get
        {
            //return base.StyleSheetTheme;
            return ConfigGlobal.GetGlobal().Theme;
        }
        set
        {
            base.StyleSheetTheme = value;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        ConfigGlobal.CheckLog();
        string uid = "0";
        string roleid = "0";
        if (ConfigGlobal.IsLog())
        {
            //this.Literal1.Text = Request.Cookies["admin_uname"].Value;
            uid = Request.Cookies["admin_uid"].Value;
            if (ConfigGlobal.GetGlobal().AllowRole)
            {
                roleid = Request.Cookies["admin_roleid"].Value;
            }
        }
        else
        {
            Helper.WriteScriptAtTop("top.location.href='default.aspx'", "nologins");
            //Response.End();
        }

        ConfigMenus menus = Server.MapPath("~/configs/menu.xml").XmlToObj<ConfigMenus>();

        List<ConfigMenus> child = menus.ChildMenus;
        List<ConfigMenus> RoleAllowMenu = null;
        List<ConfigMenus> AdminAllowMenu = null;
        ConfigGlobalSettings gs = ConfigGlobal.GetGlobal();
        if (gs.AllowRole)
        {
            RoleAllowMenu = child.Where(x => x.AllowRoles.Contains(roleid)).ToList();
        }
        if (gs.AllowBrowseControl)
        {
            AdminAllowMenu = child.Where(x => x.AllowAdmins.Contains(uid)).ToList();
        }

        if (gs.AllowRole && !gs.AllowBrowseControl)
        {
            this.Repeater1.DataSource = RoleAllowMenu;
        }
        else if (!gs.AllowRole && gs.AllowBrowseControl)
        {
            this.Repeater1.DataSource = AdminAllowMenu;
        }
        else if (gs.AllowBrowseControl && gs.AllowRole)
        {
            this.Repeater1.DataSource = RoleAllowMenu.Union(AdminAllowMenu);
        }
        else
        {
            this.Repeater1.DataSource = child;            
        }
        this.Repeater1.DataBind();

        
    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        HiddenField hide = e.Item.FindControl("MenuID") as HiddenField;
        if (hide != null)
        {            
            ConfigMenus menus = Server.MapPath("~/configs/menu.xml").XmlToObj<ConfigMenus>();
            ConfigMenus thisM = menus.ChildMenus.Single(x => x.ID == hide.Value);

            TreeView tree = e.Item.FindControl("MenuTree") as TreeView;
            if (tree != null)
            {
                thisM.SetupTree(tree);
            }
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Cookies["uid"].Value = string.Empty;
        Response.Cookies["uname"].Value = string.Empty;
        Response.Cookies["roleid"].Value = string.Empty;
        Helper.WriteScriptAtBottom("top.location.href='default.aspx'", "nologin");
    }
}
